package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo6ToHbase {
  def main(args: Array[String]): Unit = {

    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink计划器
      .inStreamingMode() //使用流模型
      .build()

    //创建flink sql的执行环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)


    bsTableEnv.executeSql(
      """
        |CREATE TABLE student (
        |  id STRING,
        |  name STRING,
        |  age INT,
        |  gender STRING,
        |  clazz STRING
        |) WITH (
        |  'connector' = 'filesystem',
        |  'path' = 'data/students.txt',
        |  'format' = 'csv',
        |  'csv.field-delimiter' = ','
        |)
        |
      """.stripMargin)

    /**
      * 将学生表写入到hbase
      * 1、以学号作为rowkey
      * 2、学生信息作为列
      *
      * create  'student','info'
      *
      */


    bsTableEnv.executeSql(
      """
        |CREATE TABLE hbase_student (
        | id STRING,
        | info ROW<name STRING,age INT , gender STRING ,clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181,node1:2181,node2:2181'
        |)

      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |insert into hbase_student
        |select id,ROW(name,age,gender,clazz) as info from student
        |
      """.stripMargin)


  }

}
